﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;


namespace
    DGen
{

    public static partial class DText
    {
        #region StringConversions

        /// <summary>
        /// Returns a date, as coerced from the string argument. Will raise
        /// an error, if the string cannot be coerced 
        /// ----so, use IsDate in this same class FIRST
        /// </summary>
        /// <param name="stream">string to get date value from</param>
        /// <returns>a DateTime object</returns>
        public static DateTime GetDate(string stream)
        {
            var dateValue = new DateTime();
            try
            {
                dateValue = DateTime.Parse(stream);
                return dateValue;
            }
            catch (Exception ex)
            {
                DBug.ErrorTools.ProcessError(ex);
                return dateValue;
            }
        }


        /// <summary>
        /// Returns an int, as coerced from the string argument. 
        /// Will raise an error, if the string cannot be coerced
        /// ----so, use IsNumber in this same class FIRST
        /// </summary>
        /// <param name="stream">string to get int value from</param>
        /// <returns>an int object</returns>
        public static int GetInteger(string stream)
        {
            try
            {
                int number = 0;
                if (!IsNumber(stream))
                    return number;
                //still here? check to see if it is formatted:
                if (IsFormattedNumber(stream))
                {
                    //it's formatted; replace the format characters
                    //with nothing (retain the decimal so as not to change 
                    //the intended value
                    stream = stream.Replace(m_Comma, string.Empty);
                    stream = stream.Replace(m_DollarSign, string.Empty);
                    stream = stream.Replace(m_PercentSign, string.Empty);
                    stream = stream.Replace(m_PoundSign, string.Empty);
                }
                //we've removed superfluous formatting characters, if they 
                //did exist, now let's round it/convert it, and return it:
                number = Convert.ToInt32(stream);
                return number;
            }
            catch (Exception ex)
            {
                DBug.ErrorTools.ProcessError(ex);
                return 0;
            }
        }


        /// <summary>
        /// Returns a double, as coerced from the string argument. 
        /// Will raise an error, if the string cannot be coerced
        /// ----so, use IsNumber in this same class FIRST
        /// </summary>
        /// <param name="stream">string to get double value from</param>
        /// <returns>a double object</returns>
        public static double GetDouble(string stream)
        {
            try
            {
                double number = 0;
                if (!IsNumber(stream))
                    return number;
                //still here? check to see if it is formatted:
                if (IsFormattedNumber(stream))
                {
                    //it's formatted; replace the format characters
                    //with nothing (retain the decimal so as not to change 
                    //the intended value)
                    stream = stream.Replace(m_Comma, string.Empty);
                    stream = stream.Replace(m_DollarSign, string.Empty);
                    stream = stream.Replace(m_PercentSign, string.Empty);
                    stream = stream.Replace(m_PoundSign, string.Empty);
                }


                //we've removed superfluous formatting characters, if they 
                //did exist, now let's round it/convert it, and return it:
                number = Convert.ToDouble(stream);
                return number;
            }
            catch (Exception ex)
            {
                DBug.ErrorTools.ProcessError(ex);
                return 0;
            }
        }

        #endregion
    }
}